﻿using Microsoft.EntityFrameworkCore;
using TxtManager.Core.Entities;
using TxtManager.Core.Interfaces;
using TxtManager.Core.Repository;
using TxtManager.Infrastructure.Context;

namespace TxtManager.Infrastructure.Repository;

public class EfTextRepository : ITextRepository{
    private readonly TextDbContext _dbContext;

    public EfTextRepository(TextDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    /// <summary>
    /// 添加实体
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public async Task AddAsync(TextItem entity)
    {
        await _dbContext.TextItems.AddAsync(entity);
        await _dbContext.SaveChangesAsync();
    }

    /// <summary>
    /// 删除实体
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task DeleteAsync(int id)
    {
        var entity = await _dbContext.TextItems.FindAsync(id);
        if (entity != null)
        {
            // entity.IsDeleted = true;
            entity.SoftDelete(); // 软删除
            await _dbContext.SaveChangesAsync();
        }
    }

    /// <summary>
    /// 获取实体
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<TextItem?> GetByIdAsync(int id)
    {
        return await _dbContext.TextItems.FindAsync(id);
    }

    /// <summary>
    /// 获取所有实体
    /// </summary>
    /// <returns></returns>
    public async Task<IEnumerable<TextItem>> GetAllAsync()
    {
        return await _dbContext.TextItems.OrderBy(x => x.Tag).ToListAsync();
    }

    /// <summary>
    /// 更新实体
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public async Task UpdateAsync(TextItem entity)
    {
        _dbContext.TextItems.Update(entity);
        await _dbContext.SaveChangesAsync();
    }

    /// <summary>
    /// 根据关键字搜索实体
    /// </summary>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task<IEnumerable<TextItem>> SearchByKeywordAsync(int pageIndex, int pageSize,string keyword)
    {

        var offset = (pageIndex - 1) * pageSize;
        var textItems = _dbContext.TextItems
            .Where(e => e.Title.Contains(keyword) || e.TextDescription.Contains(keyword))
            .Skip(offset)
            .Take(pageSize);

        return await textItems.OrderBy(x => x.Tag).ToListAsync();
    }

    /// <summary>
    /// 更新tag
    /// </summary>
    /// <param name="id"></param>
    /// <param name="newTag"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task UpdateTagsAsync(int id, string newTag)
    {
        await _dbContext.TextItems
            .Where(e => e.Id == id)
            .ExecuteUpdateAsync(e => e.SetProperty(x => x.Tag, newTag));
    }

    public async Task<int> CountTextItemsAsync(string keyword)
    {
        return await _dbContext.TextItems
            .CountAsync(e => e.Title.Contains(keyword) || e.TextDescription.Contains(keyword));
    }

}